/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * screenComp.java
 *
 * Created on 27-Oct-2010, 4:20:16 PM
 */
package balony;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.HashMap;
import javax.swing.border.LineBorder;

/**
 *
 * @author Barry Young
 */
public class screenComp extends javax.swing.JFrame {

    /** Creates new form screenComp */
    public HashMap<Integer[], dataTable.compData> cmp;

    public screenComp() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSeparator1 = new javax.swing.JSeparator();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        screen1Label = new javax.swing.JLabel();
        screen2Label = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jPanel1 = new javax.swing.JPanel() {
            @Override public void paintComponent(Graphics g) {
                super.paintComponent(g);    // paints background
                displayPlot(g);

                // do your drawing here
            }
        }
        ;
        jComboBox2 = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jSeparator1.setOrientation(javax.swing.SwingConstants.VERTICAL);

        jLabel1.setText("Screen 1:");

        jLabel2.setText("Screen 2:");

        screen1Label.setText("---------------------------------------------");

        screen2Label.setText("-----------------------------------------------");

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Table", "Scatter Plot" }));

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanel1.setPreferredSize(new java.awt.Dimension(300, 300));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 592, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 305, Short.MAX_VALUE)
        );

        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Ratio", "Log(2)", "Log(10)" }));
        jComboBox2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 594, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(screen1Label, javax.swing.GroupLayout.PREFERRED_SIZE, 189, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(28, 28, 28)
                        .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(screen2Label, javax.swing.GroupLayout.PREFERRED_SIZE, 189, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(154, 154, 154)
                        .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(10, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(jLabel2)
                        .addComponent(screen1Label)
                        .addComponent(screen2Label)))
                .addGap(69, 69, 69)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 307, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jComboBox2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jComboBox2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox2ActionPerformed
        displayPlot(jPanel1.getGraphics());
    }//GEN-LAST:event_jComboBox2ActionPerformed

    /**
     * @param args the command line arguments
     */
//    public static void main(String args[]) {
//    }

    public void displayPlot(Graphics ga) {

        Graphics2D g = (Graphics2D) ga;

        final Double l2 = Math.log(2);

        int width = jPanel1.getWidth();
        int height = jPanel1.getHeight();

        g.setColor(Color.red);
        g.setBackground(Color.white);

        g.clearRect(0, 0, jPanel1.getWidth(), jPanel1.getHeight());
        jPanel1.setBorder(new LineBorder(Color.BLACK));

        for (Integer[] i : cmp.keySet()) {
            dataTable.compData c = cmp.get(i);

            Double c1 = Balony.mean(c.ctrl1);
            Double c2 = Balony.mean(c.ctrl2);
            Double e1 = Balony.mean(c.exp1);
            Double e2 = Balony.mean(c.exp2);

            Double r1 = (c1 > 0 ? e1 / c1 : 0);
            Double r2 = (c2 > 0 ? e2 / c2 : 0);

            int n = jComboBox2.getSelectedIndex();

            if (n == 0) {
                g.setColor(Color.black);
                g.drawLine(0, height / 2, width, height / 2);
                g.drawLine(width / 2, 0, width / 2, height);
                g.setColor(Color.red);
                g.drawLine((int) (r1 * width / 2), (int) (height - r2 * height / 2), (int) (r1 * width / 2), (int) (height - r2 * height / 2));

//                Slows things down to much with thousands of labels:
//                JLabel j=new JLabel();
//                jPanel1.add(j);
//                j.setLocation((int)(r1*width/2),(int)(height-r2*height/2));
//                j.setToolTipText(c.gene);
//                j.setBounds((int)(r1*width/2)-5,(int)(height-r2*height/2)-5,10,10);
            }

            if (n == 1) {

                r1 = Math.log(r1) / l2;
                r2 = Math.log(r2) / l2;
                g.setColor(Color.black);
                g.drawLine(0, height / 2, width, height / 2);
                g.drawLine(width / 2, 0, width / 2, height);
                g.setColor(Color.red);

                g.drawLine((int) (r1 * (width / 10) + width / 2), (int) ((height / 2) - r2 * (height / 10)),
                        (int) (r1 * (width / 10) + width / 2), (int) ((height / 2) - r2 * (height / 10)));

            }

            if (n == 2) {
                r1 = Math.log(r1);
                r2 = Math.log(r2);
                g.setColor(Color.black);
                g.drawLine(0, height / 2, width, height / 2);
                g.drawLine(width / 2, 0, width / 2, height);
                g.setColor(Color.red);

                g.drawLine((int) (r1 * width / 3 + width / 2), (int) (height / 2 - r2 * width / 3),
                        (int) (r1 * width / 3 + width / 2), (int) (height / 2 - r2 * width / 3));
            }
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    public javax.swing.JPanel jPanel1;
    private javax.swing.JSeparator jSeparator1;
    public javax.swing.JLabel screen1Label;
    public javax.swing.JLabel screen2Label;
    // End of variables declaration//GEN-END:variables
}
